
let currency = require("../../utils/formatCurrency").currency;
let handleNavigationBar = require("../../utils/formatCurrency").handleNavigationBar
const app = getApp();
import * as echarts from '../../ec-canvas/echarts';

Page({
  data: {
    ec: {
      lazyLoad: true
    },
    chartData:null,
    total:0,
    optionData:[],
    taxList: [
        {title:'收入（元）',value:'income'},
        {title:'增值税',value:'vaddedTax'},
        {title:'消费税',value:'saleTax'},
        {title:'印花税',value:'stampTax'},
        {title:'城建税',value:'conTax'},
        {title:'教育费附加',value:'eduSurtax'},
        {title:'地方教育费附加',value:'locEduTax'},
        {title:'个人所得税',value:'psIncTax'},
        {title:'文化建设费',value:'culFee'},
        {title:'所得税',value:'incomeTax'}
    ],
    showChart: true
  },

  onLoad: function (options) {
    handleNavigationBar(app.globalData.company)
    wx.showLoading({
      title: '加载中',
    })
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/taxs/' + app.globalData.accountBookId + '/find?period='+options.data,
      method: 'GET',
      header: {
        token: app.globalData.token,
        version: app.globalData.version
      },
      success: function (res) {
        that.handleData(res.data)
      }
    })
  },

  handleData(data){
    for (let i = 0; i < this.data.taxList.length;i++){
      this.data.total += Number(data[this.data.taxList[i].value])
      this.data.taxList[i].tax = currency(data[this.data.taxList[i].value],false,true)
      
    }

    wx.hideLoading() 

    if (this.data.total!==0){
      for (let i = 0; i < this.data.taxList.length; i++) {
        let num = Number(data[this.data.taxList[i].value]);
        let percent = (num / this.data.total*100).toFixed(2)
        this.data.optionData.push({
          value: Number(data[this.data.taxList[i].value]), 
          name: this.data.taxList[i].title + percent+ '%'
        })
      }

      // 获取组件 初始化图表
      this.selectComponent('#mychart-dom-bar').init((canvas, width, height) => {
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        this.loadChart(chart);
        return chart;
      });
    }else{
      this.data.showChart = false;
      this.setData({
        showChart: this.data.showChart
      })
    }

    this.setData({
      taxList: this.data.taxList
    })
  },
  loadChart(chart){
    let silent = app.globalData.platform.toLowerCase() === 'android' ? true : false
    var option = {
      graphic: [
        {
          type: 'text',
          left: 'center',
          top: '44%',
          style: {
            text: '税费合计',
            textAlign: 'center',
            fill: '#8a8a8a',
            fontSize: 12
          },
        },
        {
          type: 'text',
          left: 'center',
          top: '52%',
          style: {
            text: currency(this.data.total),
            textAlign: 'center',
            fontSize: 16
          }
        },
      ],
      series: [
        {
          type: 'pie',
          radius: ['45%', '70%'],
          data: this.data.optionData,
          minAngle: 3,
          silent: silent,
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          },
          labelLine: {
            length: 10,
            length2: 10
          },
          color: ['#3d731b', '#3283bb', '#529fd3', '#7addfa', '#89ce33', '#fea000', '#ff9f7f', '#fb7293', '#e062ae', '#e690d1']
        }
      ]
    };
    chart.setOption(option);
  }
})